package com.example.roleplay.controller;

import com.example.roleplay.dto.BatchSessionCreateRequest;
import com.example.roleplay.dto.BatchSessionCreateResponse;
import com.example.roleplay.dto.SessionCreateRequest;
import com.example.roleplay.dto.SessionCreateResponse;
import com.example.roleplay.service.SessionService;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * 会话管理API控制器
 */
@RestController
@RequestMapping("/api/chat")
@RequiredArgsConstructor

public class SessionController {

    private final SessionService sessionService;

    @PostMapping("/session")

    public ResponseEntity<SessionCreateResponse> createSession(
            @Valid @RequestBody SessionCreateRequest request) {
        try {
            SessionCreateResponse response = sessionService.createSession(request);
            return ResponseEntity.ok(response);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().build();
        }
    }

    @PostMapping("/sessions/batch")

    public ResponseEntity<BatchSessionCreateResponse> createBatchSessions(
            @Valid @RequestBody BatchSessionCreateRequest request) {
        try {
            BatchSessionCreateResponse response = sessionService.createBatchSessions(request);
            return ResponseEntity.ok(response);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().build();
        }
    }

    @DeleteMapping("/session/{sessionId}")

    public ResponseEntity<Void> endSession(
            @PathVariable String sessionId) {
        if (!sessionService.isSessionValid(sessionId)) {
            return ResponseEntity.notFound().build();
        }
        sessionService.endSession(sessionId);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/session/{sessionId}/status")

    public ResponseEntity<String> getSessionStatus(
            @PathVariable String sessionId) {
        if (sessionService.isSessionValid(sessionId)) {
            return ResponseEntity.ok("ACTIVE");
        } else {
            return ResponseEntity.ok("INVALID");
        }
    }
}